package studio.raptor.ddal.dashboard.api;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import studio.raptor.ddal.dashboard.ddl.DdalDemo;
import studio.raptor.ddal.jdbc.RaptorDataSource;
import javax.sql.DataSource;
import java.sql.Connection;

/**
 * @author Sam
 * @since 3.0.0
 */
@RestController
public class DdalDemoController {

  private static DataSource dataSource;
  private static Connection connection;
  private static DdalDemo ddalDemo;

  static{
    try{
      if(null == dataSource) {
        dataSource = new RaptorDataSource("quickstart", "oracle");
      }
      if(null == connection){
        connection = dataSource.getConnection();
      }
      if(null == ddalDemo){
        ddalDemo=new DdalDemo();
        ddalDemo.init( dataSource,connection );
      }
    }catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  @RequestMapping("/test")
  public String index() {
    System.out.println( "HelloController.index dataSource" +dataSource.toString());
    System.out.println( "HelloController.index connection" +connection.toString() );
    return "Greetings from Spring Boot!";
  }

  @RequestMapping(value = "/test4111/{i}")
  @ResponseBody
  public String test4111(@PathVariable int i) throws Exception{
    ddalDemo.test4111( i );
    return "操作成功";
  }

  @RequestMapping(value = "/test4112/{i}&{id}")
  @ResponseBody
  public String test4112(@PathVariable int i,@PathVariable long id) throws Exception{
    ddalDemo.test4112( i,id );
    return "操作成功";
  }

  @RequestMapping(value = "/test4113/{i}&{id}")
  @ResponseBody
  public String test4113(@PathVariable int i,@PathVariable long id) throws Exception{
    ddalDemo.test4113( i,id );
    return "操作成功";
  }




}
